package com.jsgt.greekfire.ui.mine.assetsmanage.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AssetsBean

/**
 * 资产管理适配器
 * author fhx
 * create time 2020/4/24 17:05
 **/
class AssetsManageAdapter : BaseAdapter<AssetsBean>() {

    private var onCheckedListener: OnCheckedListener? = null

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var icon_iv: ImageView? = null
        var coin_name_tv: TextView? = null
        var state_switch: SwitchCompat? = null

        init {
            icon_iv = itemView.findViewById(R.id.icon_tv)
            coin_name_tv = itemView.findViewById(R.id.coin_name_tv)
            state_switch = itemView.findViewById(R.id.state_switch)
        }

        fun bindData(bean: AssetsBean) {
            coin_name_tv?.text = bean.assetName
            when (bean.assetGuid) {
                "1" -> {
                    icon_iv?.setImageResource(R.mipmap.icon_glb)
                }
                "2" -> {
                    icon_iv?.setImageResource(R.mipmap.icon_usdt)
                }
                "3" -> {
                    icon_iv?.setImageResource(R.mipmap.icon_gft)
                }

            }
            state_switch?.isChecked = bean.display == 1
            state_switch?.setOnCheckedChangeListener { _, isChecked ->
                onCheckedListener?.onChecked(isChecked, bean)
            }
        }
    }

    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_assets_manage, parent, false)
        return VH(view)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VH
        viewHolder.bindData(dataList()[position])
    }

    fun setOnCheckedListener(onCheckedListener: OnCheckedListener) {
        this.onCheckedListener = onCheckedListener
    }

    interface OnCheckedListener {
        fun onChecked(isChecked: Boolean, bean: AssetsBean)
    }
}